package com.java2_5;

import javax.swing.text.Style;
import java.util.Random;
import java.util.Scanner;

public class java2_5 {
    //冒泡排序
//    public static void main(String[] args) {
//        int[] arr = {2, 4, 3, 5, 6, 1, 8, 7, 9, 10};
//        for (int i = 0; i < arr.length - 1; i++) {
//            boolean flag = true;
//            for (int j = 0; j < arr.length - 1 - i; j++) {
//                if (arr[j] > arr[j + 1]){
//                    int temp = arr[j];
//                    arr[j] = arr[j+1];
//                    arr[j + 1] = temp;
//                    flag = false;
//                }
//            }
//            if(flag)
//                break;
//        }
//        for (int i = 0; i < arr.length; i++) {
//            System.out.print(arr[i] + " ");
//        }
//    }
//    public static void main(String[] args) {
//        //生成随机数
//        Random r = new Random(); //构造器
//        int luckNumber = r.nextInt(10) + 1;
//        System.out.println(luckNumber);
//    }
//    public static void main(String[] args) {
//        //随机排名
//        //遍历数组每一个元素，将其与随机生成的索引值的元素互换位置；
//        String[] name = {"zhansan", "lisi", "wangwu", "zhaoliu", "coco"};
//        Random r  = new Random();
//        for (int i = 0; i < name.length; i++) {
//            int luckNumber = r.nextInt(5);
//            String temp = name[i];
//            name[i] = name[luckNumber];
//            name[luckNumber] = temp;
//        }
//        for (int j = 0; j < name.length; j++) {
//            System.out.print(name[j] + " ");
//        }
//    }
//    public static void main(String[] args) {
//        //类、对象
//        classMember member = new classMember();
//        member.name = "zhangsan";
//        member.age = 20;
//        member.index = 100;
//        member.sex = '男';
//        System.out.println(member);
//        System.out.println(member.name);
//        System.out.println(member.age);
//        System.out.println(member.index);
//        System.out.println(member.sex);
//        member.text();
//    }
//    public static void main(String[] args) {
////        Student S = new Student();
////        System.out.println(S.name);
////        System.out.println(S.age);
////        System.out.println(S.index);
////        System.out.println(S.phone);
//        Student S = new Student("zhangsan",20,55,123456);
//        System.out.println(S.name);
//        System.out.println(S.age);
//        System.out.println(S.index);
//        System.out.println(S.phone);
//        System.out.println("-----------------------");
//        Student stu = new Student();
//        stu = S;
//        stu.name = "coco";
//        System.out.println(S.name);
//        System.out.println(stu);
//        System.out.println(S);
//    }
//    public static void main(String[] args) {
//        javabeanStu stu = new javabeanStu();
//        stu.setName("zhangsan");
//        System.out.println(stu.getName());
//        stu.setAge(20);
//        stu.setIndex(12138);
//        stu.setSex('男');
//        System.out.println(stu.getAge());
//        System.out.println(stu.getIndex());
//        System.out.println(stu.getSex());
//
//    }
//    public static void main(String[] args) {
//        //if else语句 == xxx ? : ；
////        int sum = 100;
////        if (sum < 100) {
////            System.out.println("too small");
////        } else {
////            System.out.println("OK");
////        }
////        int sum = 100;
////        System.out.println(sum < 100 ? "too small" : "Ok");
////    }
//    public static void main(String[] args) {
//
//    }
    //购物车购物商品模拟
    public static void main(String[] args) {
        //菜单栏的打印
        Goods[] shopCar = new Goods[100];
        while (true) {
            menu();
            System.out.print("请输入您的想要执行的命令：");
            Scanner sc = new Scanner(System.in);
            String command = sc.next();
            switch (command) {
                case "add":
                    addGoods(shopCar, sc);
                    break;
                case "query":
                    query(shopCar);
                    break;
                case "update":
                    update(shopCar, sc);
                    break;
                case "pay":
                    pay(shopCar);
                    break;
                default:
                    System.out.println("您输入的命令有误，请重新输入！");
                    break;
            }
        }

    }

    //计算总金额
    public static void pay(Goods[] shopCar) {
        double sum = 0;
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if (g != null) {
                sum += (g.price * g.number);
            }else{
                break;
            }
        }
        System.out.println("预购商品的中金额为：" + sum + "\n");
    }

    //更改对应编号的商品的数量
    public static void update(Goods[] shopCar, Scanner sc) {
        query(shopCar);
        System.out.print("请输入您所要修改的商品的编号为：");
        int index = sc.nextInt();
        boolean flag = true;
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if (g.index == index) {
                System.out.print("请输入您想要修改的购买的数量：");
                g.number = sc.nextInt();
                flag = false;
                System.out.println("您想要修改的商品的数量已经修改完毕！\n");
                break;
            }
        }
        if (flag)
            System.out.println("对不起，暂时还没有预购编号为：" + index + "的商品\n");

    }

    //打印预购商品的信息
    public static void query(Goods[] shopCar) {
        System.out.println("您预购的商品信息如下：");
        for (int i = 0; i < shopCar.length; i++) {
            Goods g = shopCar[i];
            if (g != null) {
                System.out.println(g.index + "\t\t\t" + g.name + "\t\t\t" + g.price + "\t\t\t" + g.number);
            } else {
                break;
            }
        }
        System.out.println("您的预购商品信息如上！\n");
    }

    //添加商品信息
    public static void addGoods(Goods[] shopCar, Scanner sc) {
        //商品信息的输入
        Goods g = new Goods();
        System.out.print("请输入商品的编号：");
        g.index = sc.nextInt();
        System.out.print("请输入商品的名称：");
        g.name = sc.next();
        System.out.print("请输入商品的价格：");
        g.price = sc.nextDouble();
        System.out.print("请输入商品的数量：");
        g.number = sc.nextInt();

        //将商品存储到数组中
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                shopCar[i] = g;
                break;
            }
        }
        System.out.println("商品增加完毕！\n");
    }

    //打印菜单
    public static void menu() {
        System.out.println("------------------有如下功能------------------");
        System.out.println("1.添加商品请输入add！");
        System.out.println("2.查看购物车商品信息请输入query！");
        System.out.println("3.修改购物车商品信息请输入update！");
        System.out.println("4.结算购物车商品请输入pay！");
    }


}
